<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属年级" prop="grade">
        <el-select filterable v-model="queryParams.grade" placeholder="请选择所属年级" clearable size="small">
          <el-option v-for="dict in grades" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属科目" prop="subject">
        <el-select filterable v-model="queryParams.subject" placeholder="请选择所属科目" clearable size="small">
          <el-option v-for="dict in subjects" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属年份" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入所属年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属学期" prop="semester">
        <el-select v-model="queryParams.semester" placeholder="请选择所属学期" clearable size="small">
          <el-option v-for="dict in semesters" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入资料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">      
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" @sort-change="sortChange">
      <el-table-column type="selection" width="55" align="center" />      
      <el-table-column label="所属地域" align="left" prop="region" width="120px"/>
      <el-table-column label="版本" align="left" prop="version" width="120px" sortable="custom"/>
      <el-table-column label="自定义合集" align="left" prop="collection" width="120px" sortable="custom"/>
      <el-table-column label="浏览附件" align="left" prop="url">
        <template slot-scope="scope">
          <a href="#" @click="downLoadFile(scope.row.url)">
            {{scope.row.suffixv}}
          </a>
        </template>
      </el-table-column>
      <el-table-column label="收费金额" align="center" prop="amount" width="100px" sortable="custom">
        <template slot-scope="scope">
          {{scope.row.isFree==1?'免费':'￥'+scope.row.amount}}
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="readNum" width="90px" sortable="custom"/>
      <el-table-column label="下载量" align="center" prop="downNum" width="90px" sortable="custom"/>
      <el-table-column label="上传日期" align="center" prop="createTime" width="140" sortable="custom">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>      
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />    
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/business/info";
import { downLoadZip } from "@/utils/zipdownload";
export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资料信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      grades: [],
      subjects: [],
      semesters: [],
      yesNo: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        grade: null,
        subject: null,
        age: null,
        semester: null,
        materialName: null,
        region: null,
        url: null,
        isFree: null,
        amount: null,
        readNum: null,
        downNum: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialName: [
          { required: true, message: "资料名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts('grade').then(response => {
      this.grades = response.data;
    });
    this.getDicts('subject').then(response => {
      this.subjects = response.data;
    });
    this.getDicts('semester').then(response => {
      this.semesters = response.data;
    });
    this.getDicts('yes_no').then(response => {
      this.yesNo = response.data;
    });    
  },
  methods: {
    downLoadFile(url) {
      window.open(url);
    },
    sortChange(column) {
      this.loading = true;
      if (column.prop === 'version') {
        this.queryParams.order = 'version';
      }else if (column.prop === 'collection') {
        this.queryParams.order = 'collection';
      }else if (column.prop === 'amount') {
        this.queryParams.order = 'amount';
      }else if (column.prop === 'readNum') {
        this.queryParams.order = 'read_num';
      }else if (column.prop === 'downNum') {
        this.queryParams.order = 'down_num';
      }else if (column.prop === 'createTime') {
        this.queryParams.order = 'create_time';
      }
      if (column.order === 'ascending') {
        this.queryParams.orderType = 'asc';
      } else if (column.order === 'descending') {
        this.queryParams.orderType = 'desc';
      }
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.infoList.forEach(item => {
          if (null!=item.url) {
            item.suffixv = item.url.substr(item.url.lastIndexOf('/')+1);
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询资料信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.infoList.forEach(item => {
          if (null!=item.url) {
            item.suffixv = item.url.substr(item.url.lastIndexOf('/')+1);
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        grade: null,
        subject: null,
        age: null,
        semester: null,
        materialName: null,
        region: null,
        url: null,
        isFree: null,
        amount: null,
        readNum: null,
        downNum: null,
        userId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资料信息编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const ids = this.ids;
      this.$confirm('是否确认导出选中的资料?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          downLoadZip('/system/info/export/'+ids, "资料");
        }).then(response => {
          this.download(response.msg);
        })
      /*this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)*/
    }
  }
};
</script>
